﻿namespace BattleShip.BattleShipBot
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    public class Bot : IAttacker
    {
        private string lastAttackPosstion;
        private bool isShipFound;
        private BotForFoundedShip botForFoundedShip;
        private Playground playground;

        public Bot()
        {
            isShipFound = false;
            playground = new Playground();
        }

        public string Attack()
        {
            string coordinate = string.Empty;
            if (isShipFound)
            {
                coordinate = botForFoundedShip.Attack();
            }
            else
            {
                coordinate = GetPossitionForAttack(playground.GetCountOfMatrixElelemnts());
            }

            lastAttackPosstion = coordinate;
            return coordinate;
        }

        public void GetReportForAttackPosition(Dictionary<string, StatmentOfPositionInMatrix> returnStat)
        {
            foreach (var coordinate in returnStat)
            {
                if (returnStat[coordinate.Key] == StatmentOfPositionInMatrix.Empty)
                {
                    if (isShipFound)
                    {
                        botForFoundedShip.GetReportForAttackPosition(returnStat);
                    }

                    playground.ChangeMatrixStatment(new string[] { coordinate.Key }, StatmentOfPositionInMatrix.Empty);
                }
                else if (returnStat[coordinate.Key] == StatmentOfPositionInMatrix.Hitted)
                {
                    if (!isShipFound)
                    {
                        botForFoundedShip = new BotForFoundedShip(coordinate.Key, playground);
                    }
                    else
                    {
                        botForFoundedShip.GetReportForAttackPosition(returnStat);
                    }

                    isShipFound = true;
                }
                else if (returnStat[coordinate.Key] == StatmentOfPositionInMatrix.Destroyed)
                {
                    if (!isShipFound)
                    {
                        playground.ChangeMatrixStatment(new string[] { coordinate.Key }, StatmentOfPositionInMatrix.Destroyed);
                    }
                    else
                    {
                        botForFoundedShip.GetReportForAttackPosition(returnStat);
                        List<string> hittedPosition = new List<string>();
                        foreach (var destroyedCoordinate in returnStat)
                        {
                            hittedPosition.Add(destroyedCoordinate.Key);
                        }

                        playground.ChangeMatrixStatment(hittedPosition.ToArray(), StatmentOfPositionInMatrix.Destroyed);
                    }

                    isShipFound = false;
                }
            }
        }

        private string GetPossitionForAttack(int allElements)
        {
            Random rand = new Random();
            string coordinatesStr = string.Empty;

            int posstion = rand.Next(allElements);
            int coordinates = playground.GetElementForAttack(posstion);
            int col = coordinates % 10;
            int row = coordinates / 10;
            coordinatesStr = row + " " + col;
            return coordinatesStr;
        }
    }
}
